﻿using System;
using System.Collections;

namespace NeuralNetwork
{
    public class Log
    {
        public delegate void WriteLine(string line);

        private WriteLine log = null;
        private LogLevel level = LogLevel.LOG_ALL;
        private string logString = "";

        public enum LogLevel
        { 
            LOG_ERROR,
            LOG_WARNING,
            LOG_INFO,
            LOG_DETAILS,
            LOG_ALL
        }

        public void SetWriteFunction(WriteLine wl)
        {
            log = wl;
        }

        public LogLevel Level
        {
            get { return level; }
            set { level = value; }
        }

        public override string ToString()
        {
            return logString;
        }

        public void PrintList(string name, IList list)
        {
            PrintList(name, list, LogLevel.LOG_ALL);
        }

        public void PrintList(string name, IList list, LogLevel level)
        {
            if (level > this.level) return;

            string s = "";
            foreach (object obj in list)
            {
                if (s != "") s += ", ";
                s += obj.ToString();
            }           
            Print("- " + name + ": { " + s + " }", level);
        }

        public void Print(string s)
        {
            Print(s, LogLevel.LOG_DETAILS);  
        }

        public void Print(string s, LogLevel level)
        {
            if (level > this.level) return;

            string st = "[" + DateTime.Now.ToLongTimeString() + "] " + s;
            logString += st;
            if (log != null)
            {
                log(st);
            }
        }

    }
}
